package com.woshua.gsonmodel

import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import android.util.Log
import com.google.gson.Gson
import com.google.gson.GsonBuilder

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun useAppContext() {
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getTargetContext()
        assertEquals("com.woshua.gsonmodel", appContext.packageName)
    }

    @Test
    fun testDefault()
    {
        val jsonStr="""
            {
              "name":"赵宏图",
              "age":""
            }
        """.trimIndent()

//        val gson=Gson()
//        不兼容格式错误
//        val user = gson.fromJson<User>(jsonStr, User::class.java)
        val user=GsonBuilder().registerTypeAdapter(Int::class.java,IntDefaultAdapter())
                .create().fromJson<User>(jsonStr,User::class.java)
        Log.d("User",user.toString())
    }

    @Test
    fun testListDefaultEmpty()
    {
        val jsonStr="""
            {
              "username":"赵宏图",
              "age":"18",
              "languages":null
            }
        """.trimIndent()

        val user=GsonBuilder()
                .registerTypeHierarchyAdapter(List::class.java,
                        ArraySecurityAdapter())
                .create()
                .fromJson<User>(jsonStr,User::class.java)

        Log.d("User",user.toString())
    }

    /**
     * GsonBuilder的使用 格式化输出
     */
    @Test
    fun testPrintJson()
    {
        val user=User()
        user.age=12
        user.name="赵宏图"
        user.languages.add("语文")
        user.languages.add("数学")

        val gson = GsonBuilder().setPrettyPrinting().create()
//        val gson=Gson()
        val jsonStr=gson.toJson(user)
        Log.d("User",jsonStr)
    }

    /*
    * GsonBuilder 序列化为null的字段
    * */
    @Test
    fun testPrintNull()
    {
        val user=User()
        user.age=12
        user.name=null
        user.languages.add("语文")
        user.languages.add("数学")

        val gson = GsonBuilder().serializeNulls().setPrettyPrinting().create()
//        val gson=Gson()
        val jsonStr=gson.toJson(user)
        Log.d("User",jsonStr)
    }

    /**
    * GsonBuilder:生成不可直接解析的 json字符串  generateNonExecutableJson 会多出 )]}'
    * */
    @Test
    fun testPrintExecutableJson()
    {
        val user=User()
        user.age=12
        user.name=null
        user.languages.add("语文")
        user.languages.add("数学")

        val gson = GsonBuilder().serializeNulls().generateNonExecutableJson().setPrettyPrinting().create()
//        val gson=Gson()
        val jsonStr=gson.toJson(user)
        Log.d("User",jsonStr)
    }

    /**
    * GsonBuilder 禁止html标签进行转义
    * */
    @Test
    fun testDisableCovertHtml()
    {
        val user=User()
        user.age=12
        user.name="""
            <font color=<\"#fbfbfb"\>
        """.trimIndent()
        user.languages.add("语文")
        user.languages.add("数学")

        val gson = GsonBuilder().serializeNulls().disableHtmlEscaping().setPrettyPrinting().create()
//        val gson=Gson()
        val jsonStr=gson.toJson(user)
        Log.d("User",jsonStr)
    }
}
